set color to bg+/b
@ 7,1 clea to 23,78
do boxt with 8,2,'ANALYSIS OF CLINIC FREQUENTATION','W+','RB',.T.,.T.
set color to bg+/b
if lastkey()=27
   return
endif
dr1='n:'
dr2='q:'
f1='patient'
f2='revenuex'
f3='pat_freq'
sele 1
set exclu off
use &dr1&f1 index &dr1&f1
sele 2
set exclu off
use &dr2&f2
go bott
endate=date_visit
begdate=endate-365
DO BOXT WITH 13,2,'PATIENTS and VISITS RECORDED BETWEEN : '+DTOC(BEGDATE+1)+' a0nd '+dtoc(endate),'W+','B',.F.,.F.
STORE 0 TO TPAT,TVIS
sele 2
set exclu off
use &dr2&f2 index &dr2&f2
sele 3
set exclu off
use &dr2&f3
if net_use('&dr2&f3',.t.,'3',10)
   ZAP
endif
sele 2
go top
do while .not. eof() .AND. LASTKEY()<>27
   kode=pat_filcod
   DO BOXT WITH 15,2,'PATIENT CODE : '+kode,'W+','B',.F.,.F.
   pname=pat_name
   pfname=pat_f_name
   hlth=healthline
   meds=med_schm
   store 0 to tfee,tpay,thl,tms,tpha,tpro,nvis
   do while pat_filcod=kode
      if date_visit>begdate
         tfee=tfee+total_fee
         tpay=tpay+total_pay
         thl=thl+total_hl
         tms=tms+total_ms
         tpha=tpha+total_phar
         tpro=tpro+total_proc
         nvis=nvis+1
         TVIS=TVIS+1
         DO BOXT WITH 19,2,'TOTAL NUMBER of RECORDED VISITS : '+STR(TVIS,6,0),'W+','B',.F.,.F.
      endif
      skip
   enddo
   IF NVIS>0
      sele 3
      if fil_lock(0)
         appe blank
      endif
      if rec_lock(0)
         repl pat_filcod with kode,pat_name with pname,pat_f_name with pfname,healthline with hlth,med_scheme with meds
         repl total_fee with tfee,total_pay with tpay,total_hl with thl,total_ms with tms,total_proc with tpro,total_phar with tpha,nber_visit with nvis
      endif
      unlock
      DO BOXT WITH 17,2,'NUMBER of PATIENT RECORDED : '+STR(RECNO(),6,0),'W+','B',.F.,.F.
      DO BOXT WITH 21,2,'AVERAGE NUMBER of VISITS per PATIENT : '+STR(TVIS/RECNO(),6,2),'W+','B',.F.,.F.
      sele 2
   ENDIF
enddo
SET COLOR TO W+/B
@ 12,1 CLEA TO 23,78
DO BOXF WITH 14,24,'PROCESSING ...','GR+','R','GR+*',.T.,.T.
SELE 3
totpat=reccount()

STORE 0 TO TFEE1,TFEE2,TFEE3,TFEE4,TFEE5,TFEE6,TFEE7,TFEE8,TFEE9,TFEE10,TFEE11,TFEE12,TFEES,TPAY1,TPAY2,TPAY3,TPAY4,TPAY5,TPAY6,TPAY7,TPAY8,TPAY9,TPAY10,TPAY11,TPAY12,TPAYS
STORE 0 TO TPHA1,TPHA2,TPHA3,TPHA4,TPHA5,TPHA6,TPHA7,TPHA8,TPHA9,TPHA10,TPHA11,TPHA12,TPHAS,TPRO1,TPRO2,TPRO3,TPRO4,TPRO5,TPRO6,TPRO7,TPRO8,TPRO9,TPRO10,TPRO11,TPRO12,TPROS
STORE 0 TO THL1,THL2,THL3,THL4,THL5,THL6,THL7,THL8,THL9,THL10,THL11,THL12,THLS,TMS1,TMS2,TMS3,TMS4,TMS5,TMS6,TMS7,TMS8,TMS9,TMS10,TMS11,TMS12,TMSS
STORE 0 TO VIS1,VIS2,VIS3,VIS4,VIS5,VIS6,VIS7,VIS8,VIS9,VIS10,VIS11,VIS12,VISS,PL1,pl2,totvis
DO WHILE .NOT. EOF()
   DO CASE
      CASE NBER_VISIT=1
         VIS1=VIS1+1
         TFEE1=TFEE1+TOTAL_FEE
         TPAY1=TPAY1+TOTAL_PAY
         THL1=THL1+TOTAL_HL
         TMS1=TMS1+TOTAL_MS
         TPHA1=TPHA1+TOTAL_PHAR
         TPRO1=TPRO1+TOTAL_PROC
         totvis=totvis+1
      CASE NBER_VISIT=2
         VIS2=VIS2+1
         TFEE2=TFEE2+TOTAL_FEE
         TPAY2=TPAY2+TOTAL_PAY
         THL2=THL2+TOTAL_HL
         TMS2=TMS2+TOTAL_MS
         TPHA2=TPHA2+TOTAL_PHAR
         TPRO2=TPRO2+TOTAL_PROC
         totvis=totvis+2
      CASE NBER_VISIT=3
         VIS3=VIS3+1
         TFEE3=TFEE3+TOTAL_FEE
         TPAY3=TPAY3+TOTAL_PAY
         THL3=THL3+TOTAL_HL
         TMS3=TMS3+TOTAL_MS
         TPHA3=TPHA3+TOTAL_PHAR
         TPRO3=TPRO3+TOTAL_PROC
         totvis=totvis+3
      CASE NBER_VISIT=4
         VIS4=VIS4+1
         TFEE4=TFEE4+TOTAL_FEE
         TPAY4=TPAY4+TOTAL_PAY
         THL4=THL4+TOTAL_HL
         TMS4=TMS4+TOTAL_MS
         TPHA4=TPHA4+TOTAL_PHAR
         TPRO4=TPRO4+TOTAL_PROC
         totvis=totvis+4
      CASE NBER_VISIT=5
         VIS5=VIS5+1
         TFEE5=TFEE5+TOTAL_FEE
         TPAY5=TPAY5+TOTAL_PAY
         THL5=THL5+TOTAL_HL
         TMS5=TMS5+TOTAL_MS
         TPHA5=TPHA5+TOTAL_PHAR
         TPRO5=TPRO5+TOTAL_PROC
         totvis=totvis+5
      CASE NBER_VISIT=6
         VIS6=VIS6+1
         TFEE6=TFEE6+TOTAL_FEE
         TPAY6=TPAY6+TOTAL_PAY
         THL6=THL6+TOTAL_HL
         TMS6=TMS6+TOTAL_MS
         TPHA6=TPHA6+TOTAL_PHAR
         TPRO6=TPRO6+TOTAL_PROC
         totvis=totvis+6
      CASE NBER_VISIT=7
         VIS7=VIS7+1
         TFEE7=TFEE7+TOTAL_FEE
         TPAY7=TPAY7+TOTAL_PAY
         THL7=THL7+TOTAL_HL
         TMS7=TMS7+TOTAL_MS
         TPHA7=TPHA7+TOTAL_PHAR
         TPRO7=TPRO7+TOTAL_PROC
         totvis=totvis+7
      CASE NBER_VISIT=8
         VIS8=VIS8+1
         TFEE8=TFEE8+TOTAL_FEE
         TPAY8=TPAY8+TOTAL_PAY
         THL8=THL8+TOTAL_HL
         TMS8=TMS8+TOTAL_MS
         TPHA8=TPHA8+TOTAL_PHAR
         TPRO8=TPRO8+TOTAL_PROC
         totvis=totvis+8
      CASE NBER_VISIT=9
         VIS9=VIS9+1
         TFEE9=TFEE9+TOTAL_FEE
         TPAY9=TPAY9+TOTAL_PAY
         THL9=THL9+TOTAL_HL
         TMS9=TMS9+TOTAL_MS
         TPHA9=TPHA9+TOTAL_PHAR
         TPRO9=TPRO9+TOTAL_PROC
         totvis=totvis+9
      CASE NBER_VISIT=10
         VIS10=VIS10+1
         TFEE10=TFEE10+TOTAL_FEE
         TPAY10=TPAY10+TOTAL_PAY
         THL10=THL10+TOTAL_HL
         TMS10=TMS10+TOTAL_MS
         TPHA10=TPHA10+TOTAL_PHAR
         TPRO10=TPRO10+TOTAL_PROC
         totvis=totvis+10
      CASE NBER_VISIT=11
         VIS11=VIS11+1
         TFEE11=TFEE11+TOTAL_FEE
         TPAY11=TPAY11+TOTAL_PAY
         THL11=THL11+TOTAL_HL
         TMS11=TMS11+TOTAL_MS
         TPHA11=TPHA11+TOTAL_PHAR
         TPRO11=TPRO11+TOTAL_PROC
         totvis=totvis+11
      CASE NBER_VISIT=12
         VIS12=VIS12+1
         TFEE12=TFEE12+TOTAL_FEE
         TPAY12=TPAY12+TOTAL_PAY
         THL12=THL12+TOTAL_HL
         TMS12=TMS12+TOTAL_MS
         TPHA12=TPHA12+TOTAL_PHAR
         TPRO12=TPRO12+TOTAL_PROC
         totvis=totvis+12
      CASE NBER_VISIT>12
         VISS=VISS+1
         TFEES=TFEES+TOTAL_FEE
         TPAYS=TPAYS+TOTAL_PAY
         THLS=THLS+TOTAL_HL
         TMSS=TMSS+TOTAL_MS
         TPHAS=TPHAS+TOTAL_PHAR
         TPROS=TPROS+TOTAL_PROC
         totvis=totvis+nber_visit
   ENDCASE
ENDDO
SET COLOR TO W+/B
@ 12,1 CLEA TO 23,78
SAVE ALL LIKE * TO Q:PATFREQ
DO BOX2 WITH 14,2,'DO YOU WANT TO :','DISPLAY','PRINT','GR+','R','GR+','N',PL1,.T.,.T.
IF PL1=1
*   DO REPFREQS
ELSE
*   DO REPFREQP
ENDIF
RETURN
